package com.java.jetpackdemo.navigation.navigation_one

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import com.java.jetpackdemo.R
import kotlinx.android.synthetic.main.fragment_home.*


class HomeFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        btn_home.setOnClickListener {
            val text = et_home.text.toString().trim()
            if (TextUtils.isEmpty(text)) {
                Toast.makeText(activity, "请输入传递的参数", Toast.LENGTH_SHORT).show()
            } else {

                //给FragmentDetail传值
                val bundle = Bundle()
                bundle.putString("my_name", text)
                val navController = Navigation.findNavController(it)
                navController.navigate(R.id.action_homeFragment_to_detailFragment, bundle)
            }
        }
    }
}
